package com.httpclient;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**官网demo
 * @author zhangsulei
 *
 */
public class Demo {

	public static void main(String[] args) throws ClientProtocolException, IOException {
		// 连接对象被response对象持有, 以保证内容通过response对象消费
		// 确保在finally代码块添加ClosableHttpResponse#close的调用
		// 值得注意的是, 如果连接没有被完全消费干净, 该连接将不能安全复用, 将会被关闭, 被连接池丢弃
		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpGet httpGet = new HttpGet("http://www.baidu.com");
		CloseableHttpResponse response1 = httpclient.execute(httpGet);
		try {
		    System.out.println(response1.getStatusLine());
		    HttpEntity entity1 = response1.getEntity();

		    // *) 引发releaseConnect()调用, reusable值取决于keepAliveStrategy判定, leased置为true
		    EntityUtils.consume(entity1);
		} finally {
		    // *) 若连接leased为false, 则releaseConnect(false)调用, 明确不可复用, leased置为true
		    // *) 若连接leased为true, 则do nothing
		    response1.close();
		}
	}

}
